package cn.xorange.commons.system.service.System.impl;

import cn.hutool.core.date.DateUtil;
import cn.xorange.commons.core.basic.service.BaseService;
import cn.xorange.commons.security.service.SecurityService;
import cn.xorange.commons.security.utils.SecurityUtils;
import cn.xorange.commons.system.entity.system.SysUser;
import cn.xorange.commons.system.entity.system.SysUserPost;
import cn.xorange.commons.system.entity.system.SysUserRole;
import cn.xorange.commons.system.mapper.system.*;
import cn.xorange.commons.system.service.System.ISysUserService;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author : yangjian
 * @date : 2022/2/3
 * @description :
 */
@Service
public class SysUserServiceImpl extends BaseService<SysUser, SysUserMapper> implements ISysUserService {

    @Resource
    SysUserPostMapper userPostMapper;
    @Resource
    SysUserRoleMapper userRoleMapper;
    @Resource
    SysUserManageRoleMapper userManageRoleMapper;
    @Resource
    SecurityService securityService;


    private static final String USER_ID = "userId";

    @Override
    public List<SysUser> getList(SysUser sysUser) {
        Long tenantId = SecurityUtils.getTenantId();
        sysUser.setTenantId(tenantId);
        return mapper.selectUserList(sysUser);
    }

    @Override
    public int resetPassword(SysUser sysUser) {
        return super.update(sysUser);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int add(SysUser sysUser){
        this.setUserPostIds(sysUser);
        this.setUserRoleIds(sysUser);
        return mapper.insert(sysUser);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int update(SysUser user){
        this.setUserPostIds(user);
        this.setUserRoleIds(user);
        int i = mapper.update(user);
        String userStr = JSON.toJSONString(user);
        securityService.refreshUserInfoCache(userStr);
        return  i;
    }

    @Override
    public List<Long> getUserPostIds(long userId){
        return userPostMapper.selectUserPostIds(userId);
    }

    @Override
    public List<Long> getUserRoleIds(long userId){
        return userRoleMapper.selectUserRoleIds(userId);
    }

    private void setUserPostIds(SysUser sysUser){
        JSONArray postIds = sysUser.getParams().getJSONArray("postIds");
        Map<String, Object> fields = new HashMap<>(1);
        fields.put(USER_ID,sysUser.getUserId());
        super.setRelationIds(fields,postIds,"postId", userPostMapper,SysUserPost.class);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void setUserRoleIds(SysUser sysUser){
        JSONArray roleIds = sysUser.getParams().getJSONArray("roleIds");
        Map<String, Object> fields = new HashMap<>(1);
        fields.put(USER_ID, sysUser.getUserId());
        super.setRelationIds(fields,roleIds, "roleId", userRoleMapper, SysUserRole.class);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteByIds(Long[] userIds){
        userPostMapper.delByUserIds(userIds);
        userRoleMapper.delByUserIds(userIds);
        userManageRoleMapper.delByUserIds(userIds);
        return super.deleteByIds(userIds);
    }

    @Override
    public int updateAvatar(long userId,String avatarPath){
        SysUser upUser = new SysUser();
        upUser.setUserId(userId);
        upUser.setAvatar(avatarPath);
        upUser.setUpdateBy(SecurityUtils.getUsername());
        upUser.setUpdateTime(DateUtil.date());
        return mapper.updateUserAvatar(upUser);
    }




}
